package com.newgrand.domain.model;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.newgrand.utils.JSONValid;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

/**
 * @author ZhaoFengjie
 * @version 1.0
 * @date 2022/8/11 16:34
 */
@Data
public class I8ReturnModel<T> {
    /**
     * 编码
     */
    @JsonProperty("Code")
    @JSONField(name = "Code")
    private String Code = "0";
    /**
     * 成功
     */
    @JsonProperty("IsOk")
    @JSONField(name = "IsOk")
    private Boolean IsOk = true;
    /**
     * 成功标识
     */
    @JsonProperty("ErrorCode")
    @JSONField(name = "ErrorCode")
    private String ErrorCode = "S";
    /**
     * 失败||成功返回消息
     */
    @JsonProperty("Message")
    @JSONField(name = "Message")
    private String Message;
    /**
     * 返回体
     */
    @JsonProperty("Data")
    @JSONField(name = "Data")
    private T Data;
    /**
     * 主键
     */
    @JsonProperty("PhId")
    @JSONField(name = "PhId")
    private String PhId;


    public static <T> I8ReturnModel<T> success() {
        return success("成功");
    }

    public static <T> I8ReturnModel<T> success(String message) {
        I8ReturnModel<T> dataResult = new I8ReturnModel<>();
        dataResult.setCode("0");
        dataResult.setIsOk(true);
        dataResult.setErrorCode("S");
        dataResult.setMessage(message);
        return dataResult;
    }

    public static <T> I8ReturnModel<T> success(String message, T data) {
        return success(message, data, "");
    }

    public static <T> I8ReturnModel<T> success(String message, T data, String phId) {
        I8ReturnModel<T> dataResult = new I8ReturnModel<>();
        dataResult.setCode("0");
        dataResult.setIsOk(true);
        dataResult.setErrorCode("S");
        dataResult.setMessage(message);
        dataResult.setData(data);
        dataResult.setPhId(phId);
        return dataResult;
    }

    public static <T> I8ReturnModel<T> error() {
        return error("失败");
    }

    public static <T> I8ReturnModel<T> error(String message) {
        I8ReturnModel<T> dataResult = new I8ReturnModel<>();
        dataResult.setCode("1");
        dataResult.setIsOk(false);
        dataResult.setErrorCode("E");
        dataResult.setMessage(message);
        return dataResult;
    }

    public static <T> I8ReturnModel<T> error(String message, T data) {
        return error(message, data, "");
    }

    public static <T> I8ReturnModel<T> error(String message, T data, String id) {
        I8ReturnModel<T> dataResult = new I8ReturnModel<>();
        dataResult.setCode("1");
        dataResult.setIsOk(false);
        dataResult.setErrorCode("E");
        dataResult.setMessage(message);
        dataResult.setData(data);
        dataResult.setPhId(id);
        return dataResult;
    }

    public static <T> I8ReturnModel<T> errorCode(String code, String message) {
        I8ReturnModel<T> dataResult = new I8ReturnModel<>();
        dataResult.setCode(code);
        dataResult.setIsOk(false);
        dataResult.setErrorCode("E");
        dataResult.setMessage(message);
        return dataResult;
    }

    @NotNull
    public static I8ReturnModel<String> getI8Return(String result) {
        if (result.contains("错误摘要")) {
            int begin = result.indexOf("<h3>") + 4;
            int end = result.indexOf("</h3>");
            result = result.substring(begin, end).trim();
        }
        return getI8Return(result, "");
    }

    @NotNull
    public static I8ReturnModel<String> getI8Return(String result, String describe) {
        if (StringUtils.isEmpty(result)) {
            throw new RuntimeException("无返结果，请检查配置参数");
        }
        if (JSONValid.isNotJSON2(result)) {
            throw new RuntimeException(result);
        }
        JSONObject dataRes = JSONObject.parseObject(result);
        if (result.contains("mainbase")) {
            dataRes = dataRes.getJSONObject("mainbase");
        }
        if ("success".equalsIgnoreCase(dataRes.getString("Status")) || "OK".equalsIgnoreCase(dataRes.getString("Status"))) {
            String phId = "";
            if (ObjectUtils.isNotNull(dataRes.get("id"))) {
                phId = dataRes.get("id").toString();
            } else if (ObjectUtils.isNotNull(dataRes.getJSONArray("KeyCodes"))) {
                phId = dataRes.getJSONArray("KeyCodes").get(0).toString();
            }
            return success("接收成功", "", phId);
        }
        if (ObjectUtils.isNull(dataRes.get("Msg"))) {
            throw new RuntimeException(result);
        }
        throw new RuntimeException(describe + dataRes.getString("Msg"));
    }
}
